import copy
import random

from observer import Observer
from key_distributor import KeyDistributor
from key_collector import KeyCollector

secret = 42
prime = 103
dist = KeyDistributor(secret, prime)
coll = KeyCollector(prime)
ranks = copy.copy(dist.ranks)
print ranks
observers = []
print "Generating of observers..."
for rank in ranks:
    for id in range(ranks[rank]*2):
        observers.append(Observer(rank + str(id), rank))
random.shuffle(observers)
for observer in observers:
    print "Observer rank: " + observer.rank + "; Observer name: " + observer.name 
print

print "Distribution of keys..."
for observer in observers:
    observer.key = dist.giveKey(observer)
    print "Observer: " + observer.name + "; key = " + str(observer.key)
print

print "Collecting keys..."
for observer in observers:
    coll.getKey(observer)

for rank in coll.keysCollected:
    print rank + ":"
    for k in coll.keysCollected[rank]:
        print k
    print

restoredSecret = coll.restoreSecret()

if restoredSecret == secret:
    print "Restored secret is equal to original one. Success!"
else:
    print "Restored secret is wrong..."
